package LearnDataStructure.a_线性结构.队列;

/**
 * @version: 1.0
 * @Author: wuqiu
 * @date: 2023-07-04 14:05
 * @description: LeetcodeLearnAlgorithm -> LearnDataStructure.a_线性结构.队列
 */
public class MyDequeList<T> extends MyLinkedListDouble<T> implements MyQueueAPI<T> {
    /**
     * 默认队尾添加新元素
     * @param element
     */
    @Override
    public void enqueue(T element) {
        super.add(element);
    }

    /**
     * 新添加功能
     * 在队首添加新元素
     * @param element
     */
    public void enqueueTop(T element) {
        QueueNode<T> p = head.next;
        QueueNode<T> newNode = new QueueNode<T>(element);
        head.next = newNode;
        p.pre = newNode;
        newNode.pre = head;
        newNode.next = p;
        super.size++;
    }

    /**
     * 默认对首删除
     * @return
     */
    @Override
    public T dequeue() {
        QueueNode<T> p = super.head.next;
        super.head.next = p.next;
        p.next.pre = super.head;
        p.next = null;
        p.pre = null;
        super.size--;
        return p.data;
    }

    /**
     * 新添加功能
     * 在队尾删除
     * @return
     */
    public T dequeueTail() {
        QueueNode<T> p = super.tail.pre;
        p.pre.next = super.tail;
        super.tail.pre = p.pre;
        p.pre = null;
        p.next = null;
        super.size--;
        return p.data;
    }

    @Override
    public int getSize() {
        return super.size;
    }

    @Override
    public boolean empty() {
        return super.size == 0;
    }

    /**
     * 默认查看队首
     * @return
     */
    @Override
    public T peek() {
        return super.head.next.data;
    }

    public T peekTail() {
        return super.tail.pre.data;
    }
}
